/*
 * @Author: 毕晨曦
 * @Date: 2024-07-30 12:31:07
 * @LastEditors: 毕晨曦
 * @LastEditTime: 2024-09-07 15:38:23
 * @FilePath: /xing-fu-da-zi-weapp/src/store/chat.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { defineStore } from "pinia";
import { ref, computed, watchEffect, reactive } from "vue";
import * as wsApi from "@/utils/wssocket";
import * as LoginApi from "@/api/appNumber";
import * as Api from "@/api/im";

export interface Message {
  content: string;
  id: number;
  recvId: string;
  sendId: string;
  sendTime: number;
  status: number;
  type: number;
}

export const userChat = defineStore("chat", () => {
  const userInfo = uni.getStorageSync("userInfo");
  const messageList = ref<Message[]>([]);

  if (!userInfo?.nid) {
    return;
  }
  console.log("userInfo", userInfo.nid);
  function init() {
    wsApi.init();
    wsApi.connect("ws://47.113.146.185:8878/im", userInfo.nid);
    wsApi.onConnect(() => {
      Api.getOfflineMessage(0);
      Api.getOfflineGroupMessage(0);
    });
    wsApi.onMessage((res: any, data: any) => {
      if (data.content && data.id) {
        messageList.value.push(data);
      }
    });
  }

  function setMessages(data: Message) {
    messageList.value.push(data);
  }

  const chatIdData = ref<{ [key: string]: boolean }>({});

  const chatMember = ref<{ [key: string]: boolean }>({});
  async function fetchMember(id: string) {
    const res = await LoginApi.getNumberData(id);
    chatMember.value[id] = res.data;
  }

  const chatData = computed(() => {
    const data: { [key: string]: any[] } = {};
    messageList.value.forEach((i: any) => {
      if (!data[i.sendId]) {
        data[i.sendId] = [];
        if (!chatIdData.value[i.sendId]) {
          chatIdData.value[i.sendId] = true;
          fetchMember(i.sendId);
        }
      }

      // 如果说话的人是自己
      if (i.sendId !== userInfo.nid) {
        // 如果没有这个人的数据
        if (!data[i.recvId]) {
          data[i.recvId] = [];
        }
        data[i.recvId].push(i);
        return;
      }
      data[i.sendId].push(i);
    });
    return data;
  });

  const data = ref<{ [key: string]: any[] }>({});

  watchEffect(() => {
    data.value = {};
    messageList.value.forEach((i: any) => {
      // 如果说话的人是自己
      if (i.sendId == userInfo.nid && !data.value[i.sendId]) {
        // 如果没有这个人的数据
        if (!data.value[i.recvId]) {
          data.value[i.recvId] = [];
        }
        data.value[i.recvId].push(i);
        if (!chatIdData.value[i.sendId]) {
          chatIdData.value[i.sendId] = true;
          fetchMember(i.sendId);
          fetchMember(i.recvId);
        }
        return;
      }

      if (!data.value[i.sendId]) {
        data.value[i.sendId] = [];
        if (!chatIdData.value[i.sendId]) {
          chatIdData.value[i.sendId] = true;
          fetchMember(i.sendId);
        }
      }
      data.value[i.sendId].push(i);
    });
  });

  return {
    messageList,
    init,
    setMessages,
    chatData,
    chatMember,
    data,
  };
});
